home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 2 / Engine / Engine.cpp next >
Encoding:
C/C++ Source or Header  |  2004-11-02  |  4.7 KB  |  153 lines

  1. //-----------------------------------------------------------------------------
  2. // Engine.h implementation.
  3. // Refer to the Engine.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Globals
  12. //-----------------------------------------------------------------------------
  13. Engine *g_engine = NULL;
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Handles Windows messages.
  17. //-----------------------------------------------------------------------------
  18. LRESULT CALLBACK WindowProc( HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam )
  19. {
  20.     switch( msg )
  21.     {
  22.         case WM_ACTIVATEAPP:
  23.             g_engine->SetDeactiveFlag( !wparam );
  24.             return 0;
  25.  
  26.         case WM_DESTROY:
  27.             PostQuitMessage( 0 );
  28.             return 0;
  29.  
  30.         default:
  31.             return DefWindowProc( wnd, msg, wparam, lparam );
  32.     }
  33. }
  34.  
  35. //-----------------------------------------------------------------------------
  36. // The engine class constructor.
  37. //-----------------------------------------------------------------------------
  38. Engine::Engine( EngineSetup *setup )
  39. {
  40.     // Indicate that the engine is not yet loaded.
  41.     m_loaded = false;
  42.  
  43.     // If no setup structure was passed in, then create a default one.
  44.     // Otehrwise, make a copy of the passed in structure.
  45.     m_setup = new EngineSetup;
  46.     if( setup != NULL )
  47.         memcpy( m_setup, setup, sizeof( EngineSetup ) );
  48.  
  49.     // Store a pointer to the engine in a global variable for easy access.
  50.     g_engine = this;
  51.  
  52.     // Prepare and register the window class.
  53.     WNDCLASSEX wcex;
  54.     wcex.cbSize        = sizeof( WNDCLASSEX );
  55.     wcex.style         = CS_CLASSDC;
  56.     wcex.lpfnWndProc   = WindowProc;
  57.     wcex.cbClsExtra    = 0;
  58.     wcex.cbWndExtra    = 0;
  59.     wcex.hInstance     = m_setup->instance;
  60.     wcex.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
  61.     wcex.hCursor       = LoadCursor( NULL, IDC_ARROW );
  62.     wcex.hbrBackground = NULL;
  63.     wcex.lpszMenuName  = NULL;
  64.     wcex.lpszClassName = "WindowClass";
  65.     wcex.hIconSm       = LoadIcon( NULL, IDI_APPLICATION );
  66.     RegisterClassEx( &wcex );
  67.  
  68.     // Initialise the COM using multithreaded concurrency.
  69.     CoInitializeEx( NULL, COINIT_MULTITHREADED );
  70.  
  71.     // Create the window and retrieve a handle to it.
  72.     // Note: Later the window will be created using a windowed/fullscreen flag.
  73.     m_window = CreateWindow( "WindowClass", m_setup->name, WS_OVERLAPPED, 0, 0, 800, 600, NULL, NULL, m_setup->instance, NULL );
  74.  
  75.     // Seed the random number generator with the current time.
  76.     srand( timeGetTime() );
  77.  
  78.     // The engine is fully loaded and ready to go.
  79.     m_loaded = true;
  80. }
  81.  
  82. //-----------------------------------------------------------------------------
  83. // The engine class destructor.
  84. //-----------------------------------------------------------------------------
  85. Engine::~Engine()
  86. {
  87.     // Ensure the engine is loaded.
  88.     if( m_loaded == true )
  89.     {
  90.         // Everything will be destroyed here (such as the DirectX components).
  91.     }
  92.  
  93.     // Uninitialise the COM.
  94.     CoUninitialize();
  95.  
  96.     // Unregister the window class.
  97.     UnregisterClass( "WindowClass", m_setup->instance );
  98.  
  99.     // Destroy the engine setup structure.
  100.     SAFE_DELETE( m_setup );
  101. }
  102.  
  103. //-----------------------------------------------------------------------------
  104. // Enters the engine into the main processing loop.
  105. //-----------------------------------------------------------------------------
  106. void Engine::Run()
  107. {
  108.     // Ensure the engine is loaded.
  109.     if( m_loaded == true )
  110.     {
  111.         // Show the window.
  112.         ShowWindow( m_window, SW_NORMAL );
  113.  
  114.         // Enter the message loop.
  115.         MSG msg;
  116.         ZeroMemory( &msg, sizeof( MSG ) );
  117.         while( msg.message != WM_QUIT )
  118.         {
  119.             if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  120.             {
  121.                 TranslateMessage( &msg );
  122.                 DispatchMessage( &msg );
  123.             }
  124.             else if( !m_deactive )
  125.             {
  126.                 // Calculate the elapsed time.
  127.                 unsigned long currentTime = timeGetTime();
  128.                 static unsigned long lastTime = currentTime;
  129.                 float elapsed = ( currentTime - lastTime ) / 1000.0f;
  130.                 lastTime = currentTime;
  131.             }
  132.         }
  133.     }
  134.  
  135.     // Destroy the engine.
  136.     SAFE_DELETE( g_engine );
  137. }
  138.  
  139. //-----------------------------------------------------------------------------
  140. // Returns the window handle.
  141. //-----------------------------------------------------------------------------
  142. HWND Engine::GetWindow()
  143. {
  144.     return m_window;
  145. }
  146.  
  147. //-----------------------------------------------------------------------------
  148. // Sets the deactive flag.
  149. //-----------------------------------------------------------------------------
  150. void Engine::SetDeactiveFlag( bool deactive )
  151. {
  152.     m_deactive = deactive;
  153. }